//******************************************************************
//******************************************************************
//**********          ANts Peer To Peer Sources        *************
//
// ANts P2P realizes a third generation P2P net. It protects your
// privacy while you are connected and makes you not trackable, hiding
// your identity (ip) and crypting everything you are sending/receiving
// from others.

// Copyright (C) 2004  Roberto Rossi

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package ants.p2p.gui;

import java.net.*;
import java.util.*;
import org.apache.log4j.*;

class InetAddressMonitorThread extends Thread {
	ConnectionAntPanel caller;
	boolean interrupt = false;

	static Logger _logger = Logger.getLogger(InetAddressMonitorThread.class
			.getName());

	public InetAddressMonitorThread(ConnectionAntPanel caller) {
		this.caller = caller;
		this.setPriority(1);
	}

	public void interrupt() {
		this.interrupt = true;
	}

	public void run() {
		while (!interrupt) {
			try {
				Thread.sleep(10000);
			} catch (InterruptedException ex) {
			}
			ArrayList currentAddresses = new ArrayList();
			InetAddress[] addresses;
			try {
				addresses = InetAddress.getAllByName(InetAddress.getLocalHost()
						.getHostName());
			} catch (Exception e) {
				addresses = new InetAddress[0];
			}
			for (int i = 0; i < addresses.length; i++) {
				currentAddresses.add(addresses[i]);
			}
			if (ants.p2p.utils.net.UPnPManager.instance().mappingsExist()
					&& !this.caller.container.sap.jCheckBox2.isSelected()) {
				try {
					caller.container.sap.resetInetaddress();
					caller.container.sap
							.addNATInetaddress(ants.p2p.utils.net.UPnPManager
									.instance().getNATAddress());
				} catch (Exception ex) {
					_logger.error("", ex);
				}
			}
			if (!ants.p2p.utils.net.UPnPManager.instance().mappingsExist()
					&& !currentAddresses.contains(caller.getLocalInetAddress())
					&& !this.caller.container.sap.jCheckBox2.isSelected()) {
				caller.localStringAddress = null;
				if (caller.warriorAnt != null)
					caller.warriorAnt.setLocalInetAddress(caller
							.getLocalInetAddress().getHostAddress());
				caller.container.sap.resetInetaddress();
			}
			if (!ants.p2p.utils.net.UPnPManager.instance().mappingsExist()
					&& !caller.isInternetPublicAddress(caller
							.getLocalInetAddress())
					&& !this.caller.container.sap.jCheckBox2.isSelected()) {
				try {
					addresses = InetAddress.getAllByName(InetAddress
							.getLocalHost().getHostName());
					InetAddress publicAddress = ConnectionAntPanel
							.searchForInternetPublicAddress(addresses);
					if (publicAddress != null) {
						caller.localStringAddress = publicAddress
								.getHostAddress();
						if (caller.warriorAnt != null)
							caller.warriorAnt.setLocalInetAddress(caller
									.getLocalInetAddress().getHostAddress());
						caller.container.sap.resetInetaddress();
						caller.firewallChecked = false;
					}
				} catch (Exception e) {
					_logger.error("", e);
				}
			}
			// Update LAN address every time, since it is wasting time checking if they are identical.
			if (caller.warriorAnt != null) {
				ants.p2p.query.ServerInfo si = new ants.p2p.query.ServerInfo(
						"", caller.warriorAnt.getLanAddress(), new Integer(
								caller.warriorAnt.getServerPort()), "");
				_logger.info("Updating UPnP LAN address: " + si);
				ants.p2p.utils.net.UPnPManager.instance().setCurrentLanAddress(
						si);
			}

			// Update LAN address if changed
			/*
			 * ants.p2p.query.ServerInfo curSi = ants.p2p.utils.net.UPnPManager
			 * .instance().getCurrentLanAddress(); if (curSi != null &&
			 * caller.warriorAnt != null) { ants.p2p.query.ServerInfo si = new
			 * ants.p2p.query.ServerInfo( "", caller.warriorAnt.getLanAddress(),
			 * new Integer( caller.warriorAnt.getServerPort()), ""); if
			 * (!si.getAddress().equals(curSi.getAddress()) ||
			 * !si.getPort().equals(curSi.getPort())) { _logger.info("Updating
			 * UPnP LAN address: " + si);
			 * ants.p2p.utils.net.UPnPManager.instance()
			 * .setCurrentLanAddress(si); } }
			 */
		}
	}
}
